package com.hyk.commonLib.common.utils.picker;

import android.content.Context;

import androidx.annotation.IntDef;

import com.hyk.commonLib.common.activity.BaseActivity;
import com.hyk.commonLib.common.exception.CustomException;
import com.hyk.commonLib.common.utils.picker.imageEngine.GlideEngine;
import com.luck.picture.lib.basic.PictureSelectionModel;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.engine.UriToFileTransformEngine;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnKeyValueResultCallbackListener;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.luck.picture.lib.utils.SandboxTransformUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import io.reactivex.rxjava3.core.Observable;

public class Picker {
    public static final int MIME_TYPE_IMAGE = 1;
    public static final int MIME_TYPE_VIDEO = 2;
    public static final int MIME_TYPE_ALL = 3;

    @IntDef({MIME_TYPE_IMAGE, MIME_TYPE_VIDEO, MIME_TYPE_ALL})
    public @interface MimeType {
    }

    public static Observable<File> getPhoto(BaseActivity<?> activity) {
        return getPhotoList(activity, 1)
                .flatMap(fileList -> Observable.just(fileList.get(0)));
    }

    public static Observable<List<File>> getPhotoList(BaseActivity<?> activity, int maxCnt) {
        return getList(activity, maxCnt, MIME_TYPE_IMAGE, 0, 0);
    }

    public static Observable<List<File>> getVideo(BaseActivity<?> activity) {
        return getList(activity, 1, MIME_TYPE_VIDEO, 0, 0);
    }

    public static Observable<List<File>> getVideo(BaseActivity<?> activity, int maxDuration, int minDuration) {
        return getList(activity, 1, MIME_TYPE_VIDEO, maxDuration, minDuration);
    }

    private static Observable<List<File>> getList(BaseActivity<?> activity, int maxCnt, @MimeType int mimeType, int maxDuration, int minDuration) {
        return Observable.create(emitter -> {
            int chooseMode = SelectMimeType.ofAll();
            if (mimeType == MIME_TYPE_IMAGE) {
                chooseMode = SelectMimeType.ofImage();
            } else if (mimeType == MIME_TYPE_VIDEO) {
                chooseMode = SelectMimeType.ofVideo();
            }

            PictureSelectionModel selectionModel = PictureSelector.create(activity)
                    .openGallery(chooseMode)
                    .setImageEngine(GlideEngine.createGlideEngine())
                    .setMaxSelectNum(maxCnt)
                    .setSandboxFileEngine(new UriToFileTransformEngine() {
                        @Override
                        public void onUriToFileAsyncTransform(Context context, String srcPath, String mineType, OnKeyValueResultCallbackListener call) {
                            if (call != null) {
                                String sandboxPath = SandboxTransformUtils.copyPathToSandbox(context, srcPath, mineType);
                                call.onCallback(srcPath, sandboxPath);
                            }
                        }
                    });

            if (maxDuration > 0) {
                //设置视频可选取的最大时长,同时也是视频可录制的最大时长
                selectionModel.setFilterVideoMaxSecond(maxDuration).setRecordVideoMaxSecond(maxDuration);
            }
            if (minDuration > 0) {
                //设置视频可选取的最小时长
                selectionModel.setFilterVideoMinSecond(maxDuration).setRecordVideoMinSecond(maxDuration);
            }

            selectionModel.forResult(new OnResultCallbackListener<LocalMedia>() {
                @Override
                public void onResult(ArrayList<LocalMedia> result) {
                    List<File> fileList = result.parallelStream().map(imageItem -> {
                        return new File(imageItem.getSandboxPath());
                    }).collect(Collectors.toList());
                    emitter.onNext(fileList);
                    emitter.onComplete();
                }

                @Override
                public void onCancel() {
                    emitter.onError(new CustomException("取消选择"));
                    emitter.onComplete();
                }
            });
        });
    }
}
